<?php 
namespace Home\Controller;

use Think\Controller;

class OrderController extends Controller
{
	public function index()
	{
		// var_dump($_SESSION);
		$old = 0;
		// 生成索引数组
		$val = array_values($_POST);
		$i=0;
		//将商品数量添加到SESSION中
		foreach ($_SESSION['Mycar'] as $k => $v) {
			$_SESSION['Mycar'][$k]['num'] = $val[$i];
			$i++;
			$old += $_SESSION['Mycar'][$k]['num']*$_SESSION['Mycar'][$k]['price'];
		}
		if(empty($old)){
			$this->error('请先购物');
		}


		//优惠劵
		// var_dump($_SESSION['Coupon']);
		//如果是空的就不用
		if(empty($_SESSION['Coupon'])){

		}else{
			$clist = [];
			$plist = [];
			$ccc;
			//筛选符合条件的优惠劵
			foreach ($_SESSION['Coupon'] as $k => $v) {
				//筛选符合条件的优惠劵的价格
				if($v['one']<$old){
					$plist[$k] = $v['one'];	
				}
			}
			//最大优惠价格
			$maxc = max($plist);
			foreach ($_SESSION['Coupon'] as $k => $v) {
				if($v['one'] == $maxc){
					$ccc = $v;
				}
			}
			$_SESSION['Usecup'] = $ccc;
		}

		$now = $old - $ccc['two'];
		$tprice = [];
		$tprice['old'] = $old;
		$tprice['now'] = $now;
		// var_dump($_SESSION);
		// var_dump($old);die;
		$this->assign('ccc',$ccc);
		$this->assign('tprice',$tprice);
		// var_dump($tprice);die;
		$this->display();
	}

	public function make()
	{
		// var_dump($_POST);
		// unset($_SESSION['User']['id']);
		// var_dump($_SESSION);die;
		/*if(empty($_POST['receiver']||$_POST['tel'])){
			$this->error('请认真填写信息!');
		}*/

		if(empty($_SESSION['User'])){
			$this->error('请先登录!',U('User/index'));
		}
		//把要用的数据存入$list
		$list = [];
		foreach ($_POST as $k => $v) {
			$list[$k] = $v;
		}
		//同上
		foreach ($_SESSION['User'] as $k => $v) {
			$list[$k] = $v;
		}
		//统计总价格
		$price = 0;
		
		foreach ($_SESSION['Mycar'] as $k => $v) {
			$price += $v['price']*$v['num'];
		}
			
		//优惠劵
		// var_dump($_SESSION['Coupon']);
		//如果是空的就不用
		if(empty($_SESSION['Usecup'])){

		}else{
			//价格减去优惠的钱
			$price = $price - $_SESSION['Usecup']['two'];
		}
		
		// var_dump($price);die;
		$list['price'] = $price;
		//下单时间
		$list['otime'] = date('Y-m-d H:i:s');
		// var_dump($list['otime']);

		//收货地址
		$addr = D('lamp_address');
		$proid = $_POST['pro'];
		$cityid = $_POST['city'];
		$areaid = $_POST['area'];
		if(empty($_POST['street'])){
			$street='';
		}else{
			$strid = $_POST['street'];
			$street = $addr->field('name')->where("id=$strid")->select();
			$street = $street[0]['name'];
		}
		$pro = $addr->field('name')->where("id=$proid")->select();
		$city = $addr->field('name')->where("id=$cityid")->select();
		$area = $addr->field('name')->where("id=$areaid")->select();
		$pro = $pro[0]['name'];
		$city = $city[0]['name'];
		$area = $area[0]['name'];
		// var_dump($city);
		$address = $pro.$city.$area.$street.$_POST['address'];
		// var_dump($address);die;
		$list['address'] = $address;


		//生成订单号
		$chars = md5(uniqid(mt_rand(), true));
			$ordernum  = substr($chars,0,8) . '-';
			$ordernum .= substr($chars,8,4) . '-';
			$ordernum .= substr($chars,12,4) . '-';
			$ordernum .= substr($chars,16,4) . '-';
			$ordernum .= substr($chars,20,12);
		$list['ordernum'] = $ordernum;

		//如果是在线付款，生成订单成功的时候就要已付款
		// var_dump($list['paytype']);
		if($list['paytype'] == 1){
			$list['paystatus'] = 1;
		}else{
			//如果是货到付款，生成订单成功的时候就要未付款
			$list['paystatus'] = 0;
		}
		// var_dump($list);die;
		//下单
		$od = D('order');
		if($od->create($list)) {
			// var_dump($od->create($list));die;
			if(!$od->add()){
				$this->error('订单生成失败！');
			}else{
				// echo $od->getLastSql();
				//若下单成功，则生成订单详情表
				$orderlist = [];
				foreach ($_SESSION['Mycar'] as $k => $v) {
					$goods = D('goods');
					//库存与销量
					$number = $goods->field('stock,sales')->where('id='.$v['gid'])->select();
					$stock = (int)$number[0]['stock'];
					$sales = (int)$number[0]['sales'];
					$num = (int)$v['num'];
					$stock = $stock-$num;
					$sales = $sales+$num;
					$change=[];
					$change['stock']=$stock;
					$change['sales']=$sales;

					$orderlist = $v;
					$orderlist['ordernum'] = $ordernum;
					$gdd = D('orderdata');
					//生成订单明细表的同时，把销量和库存计算好
					if($gdd->create($orderlist)&&$goods->create($change)){
						// var_dump($gdd->create($orderlist));
						// var_dump($goods->create($change))
						// echo 1;die;
						if($gdd->add()&&$goods->where('id='.$v['gid'])->save()){
							//清空购物车
							unset($_SESSION['Mycar'][$k]);
							$c=$_SESSION['Usecup']['id'];
							//清除用掉的优惠劵
							unset($_SESSION['Usecup']);
							unset($_SESSION['Coupon'][$c]);
							$this->success('生成订单成功!',U('Orderinfo/index'),1);
						}else{
							$this->error('生成订单失败!');
						}
					}
				}	
			}
		}else{
			$this->error($od->getError());
		}
	}
	//地址四级联动
	public function four()
	{
		$model = M('lamp_address');

		//接收查询条件
		$upid = empty($_GET['upid'])?0:$_GET['upid'];
		//查询
		$data = $model->where('upid='.$upid)->select();
		//将结果返回
		echo json_encode($data);

	}

	// public function jump()
	// {
	// 	$this->display(U('Person/order'));
	// }
}

